package com.CmJava.controller;


import com.CmJava.entity.ResultEntity;
import com.CmJava.entity.comment.CommentEntity;
import com.CmJava.entity.comment.CommentRedis;
import com.CmJava.entity.visitor.Visitor;
import com.CmJava.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

@Controller
public class CommentController {
    @Autowired
    private CommentService commentService;


    // 获取mysql评论信息
    @RequestMapping("getCommentsMysql")
    @ResponseBody
    public ResultEntity getCommentsMysql(@RequestParam("projectId") String projectId){
        ResultEntity resultEntity= commentService.getCommentsMysql(projectId);
        return resultEntity;
    }

    // 获取redis评论信息
    @RequestMapping("getCommentsRedis")
    @ResponseBody
    public ResultEntity getCommentsRedis(@RequestParam("commentKey") String commentKey){
        if (commentKey=="--"){
            return ResultEntity.failureWithoutMessage();
        }
        ResultEntity resultEntity= commentService.getCommentsRedis(commentKey);
        return resultEntity;
    }

    // 发送新评论
    @RequestMapping("sendComment")
    @ResponseBody
    public ResultEntity sendComment(@RequestParam("projectId") String projectId,
                                    @RequestParam("comment") String comment,
                                    @RequestParam("commentNum") String commentNum,
                                    HttpServletRequest request){
        String key="";
        if (Integer.parseInt(commentNum)==0){
            key="p"+projectId+"_c1_for_MyWeb";
        }else {
            key="p"+projectId+"_c"+(Integer.parseInt(commentNum)+1)+"_for_MyWeb";
        }

        String commenterName = request.getRemoteAddr();
        if (commenterName.equals("127.0.0.1")){
            commenterName="博主";
        }else {
            commenterName+=" 网友";
        }
        String thisMoment = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        CommentEntity commentEntity = new CommentEntity("p"+projectId+"_c"+(Integer.parseInt(commentNum)+1)+"_1"+"_for_MyWeb",commenterName, comment, thisMoment, 0);
        ArrayList<CommentEntity> commentEntities = new ArrayList<>();
        commentEntities.add(commentEntity);
        CommentRedis commentRedis = new CommentRedis(key, commentEntities);

        ResultEntity resultEntity= commentService.sendComment(key,commentRedis,projectId);
        return resultEntity;
    }


    // 点赞评论
    @RequestMapping("thumbTa")
    @ResponseBody
    public ResultEntity thumbTa(@RequestParam("commentId") String commentId,
                                HttpServletRequest request){

        Visitor visitor = (Visitor) request.getSession().getAttribute("visitor");
        String thumbKeys = visitor.getThumbKey();
        if (!thumbKeys.equals("--")) {
            if (!thumbKeys.equals(commentId)) {
                for (String thumbKey : thumbKeys.split("-")) {
                    if (thumbKey.equals(commentId)) {
                        // 这个评论，该ip点赞过了
                        return ResultEntity.failureWithMessage("您已经点赞过了");
                    }
                }
            }else {
                return ResultEntity.failureWithMessage("您已经点赞过了");
            }
        }

        // p2_c1_1_for_MyWeb
        String[] s = commentId.split("_");
        String redisKey="";
        for (int i = 0; i < s.length; i++) {
            if (i!=2) {
                redisKey += s[i];
                if (i!=s.length-1){
                    redisKey+="_";
                }
            }
        }
        ResultEntity resultEntity= commentService.thumbTa(redisKey,commentId,request);
        return resultEntity;
    }


    // 回复评论
    @RequestMapping("replyComment")
    @ResponseBody
    public ResultEntity replyComment(@RequestParam("commentId") String commentId,
                                     @RequestParam("comment") String comment,
                                     HttpServletRequest request){
        String[] s = commentId.split("_");
        String redisKey="";
        for (int i = 0; i < s.length; i++) {
            if (i!=2) {
                redisKey += s[i];
                if (i!=s.length-1){
                    redisKey+="_";
                }
            }
        }
        ResultEntity resultEntity= commentService.replyComment(redisKey,commentId,comment,request);
        return resultEntity;
    }


    // 博客下发送评论
    @RequestMapping("sendBlogComment")
    @ResponseBody
    public ResultEntity sendBlogComment(@RequestParam("blogId") String blogId,
                                        @RequestParam("comment") String comment,
                                        @RequestParam("commentNum") String commentNum,
                                        HttpServletRequest request) {
        String key="";
        if (Integer.parseInt(commentNum)==0){
            key="b"+blogId+"_c1_for_MyWeb";
        }else {
            key="b"+blogId+"_c"+(Integer.parseInt(commentNum)+1)+"_for_MyWeb";
        }

        String commenterName = request.getRemoteAddr();
        if (commenterName.equals("127.0.0.1")){
            commenterName="博主";
        }else {
            commenterName+=" 网友";
        }
        String thisMoment = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        CommentEntity commentEntity = new CommentEntity("b"+blogId+"_c"+(Integer.parseInt(commentNum)+1)+"_1"+"_for_MyWeb",commenterName, comment, thisMoment, 0);
        ArrayList<CommentEntity> commentEntities = new ArrayList<>();
        commentEntities.add(commentEntity);
        CommentRedis commentRedis = new CommentRedis(key, commentEntities);

        ResultEntity resultEntity= commentService.sendBlogComment(key,commentRedis,blogId);
        return resultEntity;
    }

    // 获取博客的mysql评论信息
    @RequestMapping("getCommentsMysqlBlog")
    @ResponseBody
    public ResultEntity getCommentsMysqlBlog(@RequestParam("blogId") String blogId){

        ResultEntity resultEntity= commentService.getCommentsMysqlBlog(blogId);
        return resultEntity;
    }

    // 获取博客的redis评论信息
    @RequestMapping("getCommentsRedisBlog")
    @ResponseBody
    public ResultEntity getCommentsRedisBlog(@RequestParam("commentKey") String commentKey){
        if (commentKey=="--"){
            return ResultEntity.failureWithoutMessage();
        }
        ResultEntity resultEntity= commentService.getCommentsRedisBlog(commentKey);
        return resultEntity;
    }

    // 回复博客评论
    @RequestMapping("replyCommentBlog")
    @ResponseBody
    public ResultEntity replyCommentBlog(@RequestParam("commentId") String commentId,
                                     @RequestParam("comment") String comment,
                                     HttpServletRequest request){
        String[] s = commentId.split("_");
        String redisKey="";
        for (int i = 0; i < s.length; i++) {
            if (i!=2) {
                redisKey += s[i];
                if (i!=s.length-1){
                    redisKey+="_";
                }
            }
        }
        ResultEntity resultEntity= commentService.replyCommentBlog(redisKey,commentId,comment,request);
        return resultEntity;
    }
}
